//
//  CXNavigationViewController.m
//  喜马拉雅FM
//
//  Created by hcios on 2017/8/26.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "CXNavigationViewController.h"

@interface CXNavigationViewController ()<UIGestureRecognizerDelegate>

@property (nonatomic, strong) NSString * imageName;

@property (nonatomic, strong) UIWindow * window;

@end

@implementation CXNavigationViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGSize imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.width,64);
    UIGraphicsBeginImageContextWithOptions(imageSize,NO,0.0);
    [[UIColor whiteColor] set];
    UIRectFill(CGRectMake(0,0, imageSize.width, imageSize.height));
    UIImage *pressedColorImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.navigationBar setBackgroundImage:pressedColorImg forBarMetrics:UIBarMetricsDefault];//用Default

    
    // 获取系统自带滑动手势的target对象
    id target = self.interactivePopGestureRecognizer.delegate;
    
    // 创建全屏滑动手势，调用系统自带华东手势的target的action方法
    SEL internalAction = NSSelectorFromString(@"handleNavigationTransition:");
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:internalAction];
    // 设置手势代理，拦截手势触发
    pan.delegate = self;
    // 给导航控制器的view添加全屏滑动的手势
    [self.view addGestureRecognizer:pan];
    // 禁止使用系统自带的滑动手势
    self.interactivePopGestureRecognizer.enabled = NO;
}

#pragma mark - UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    return self.childViewControllers.count > 1;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    // 当非根控制器时设置导航条左侧返回按钮
    if (self.childViewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;  // 非根控制器隐藏底部tabBar
        viewController.navigationItem.hidesBackButton = YES;
        
        //设置返回按钮，只有非根控制器才需要设置返回按钮
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem backItemWithImage:[UIImage imageNamed:@"navigationButtonReturn"] highlightedImage:[UIImage imageNamed:@"navigationButtonReturnClick"] target:self action:@selector(didClickBackBarButton) title:@"返回"];
    }
    //调用父类
    [super pushViewController:viewController animated:animated];
}


#pragma mark - 点击返回按钮
-(void)didClickBackBarButton{
    [self popViewControllerAnimated:YES];
}

@end
